var FeedResultListener = function() { };

FeedResultListener.prototype = {
  handleResult: function(result) {
    var feed = result.doc.QueryInterface(Components.interfaces.nsIFeed);
    var items = feed.items;
    
    //
    var RDF = Components.classes['@mozilla.org/rdf/rdf-service;1'].getService()
				.QueryInterface(Components.interfaces.nsIRDFService);
			
			var RDFC = Components.classes['@mozilla.org/rdf/container;1'].getService()
				.QueryInterface(Components.interfaces.nsIRDFContainer);
			
			var RDFCUtils = Components.classes['@mozilla.org/rdf/container-utils;1'].getService()
				.QueryInterface(Components.interfaces.nsIRDFContainerUtils);
			
			var ds = Components.classes["@mozilla.org/rdf/datasource;1?name=in-memory-datasource"].createInstance()
				.QueryInterface(Components.interfaces.nsIRDFDataSource);
			
			// create our root nodes
			var rootNode = RDF.GetResource("urn:root");
			var seqNode = RDF.GetResource("urn:root:seq");
			
			// insert the "top" of the tree, a Seq container
			ds.Assert(rootNode,
				RDF.GetResource("http://www.bttlindia.com/RDF#subheadings"),
				seqNode, 
				true);
			
			RDFCUtils.MakeSeq(ds, seqNode);
			RDFC.Init(ds, seqNode);
    
    // Write the article information
    
    if (!items || items.length <= 0) {
      // doc.innerHTML = "<i>No news is good news!</i>";
      return;
    }
    
      var i;
      var entry;
      var uri;
      var info;
      
      for (i = 0; i < items.length; i++) {
        entry = items.queryElementAt(i, Components.interfaces.nsIFeedEntry);
        
        if (entry) {
        	uri = entry.link.resolve("");
        	
					var newURI = "urn:root:file:" + i;
					var newResource = RDF.GetResource(newURI);
					
					// add the new element
					RDFC.AppendElement(newResource);
					
					// var items = entry.items.QueryInterface(Components.interfaces.nsIPropertyBag2);
					
					ds.Assert(newResource,
						RDF.GetResource("http://www.bttlindia.com/RDF#id"),
						RDF.GetLiteral(entry.title.text), 
						true);
					
					ds.Assert(newResource,
						RDF.GetResource("http://www.bttlindia.com/RDF#name"),
						RDF.GetLiteral(entry.link.resolve("")),
						true);
					}
        }
      
      
    
    var tree = document.getElementById("my-tree");
    var db = tree.database;
    var sources = db.GetDataSources();
    
    while (sources.hasMoreElements()) {
    	db.RemoveDataSource(sources.getNext());
    }
    
		db.AddDataSource(ds);
		tree.builder.rebuild();
  }
}

_log("FeedResultListener");